<?php


class MyPdo{
	public $p = '';
	// private $hostOrDb = "mysql:host=bdm262404636.my3w.com;dbname=bdm262404636_db";
	// private $root = 'bdm262404636';
	// private $pwd = 'xiaodong123';
	private $hostOrDb = "mysql:host=localhost;dbname=translate";
	private $root = 'root';
	private $pwd = 'xiaodong123';
	public function __construct($hostOrDb='',$root='',$pwd=''){
		static $p2 = '';
		if($hostOrDb){
			$this->hostOrDb = $hostOrDb;
		}
		if($hostOrDb && $root && $pwd){
			$this->hostOrDb = $hostOrDb;
			$this->root = $root;
			$this->apwd = $pwd;
		}
		if($p2 == ''){
			try{
				$this->p = $p2 = new PDO($this->hostOrDb,$this->root,$this->pwd);
				$this->p->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
				$this->p->query("set names utf8");
			}catch(PDOException $e){
				die('数据库连接失败'.$e->getMessage());
			}
		}else{
			$this->p = $p2;
		}
		return $this->p;
	}

	/*
		@查询操作
		@parm 	$sql (string)	sql语句
		@parm 	$arr (Array)	索引数组形式的参数	
		@parm 	$type 	string	查单或多条，默认false查询多条,true为查询单条
		@return
	*/
	public function dql($sql,$arr,$type=false){
		$st = $this->p->prepare($sql);
		$st->execute($arr);
		if(!$type){
			$res = $st->fetchAll(PDO::FETCH_ASSOC);
		}else{
			$res = $st->fetch(PDO::FETCH_ASSOC);
		}
		if($res){
			return $res;
		}else{
			return false;
			//return $st->errorCode().'aaaaaaa';
		}
	}




	/*
		@增删改操作
		@parm 	$sql (string)	sql语句
		@parm 	$arr (Array)	索引数组形式的参数
		@return  
				添加操作成功时返回最后添加的id
				删除操作成功时返回受影响的行数
				修改操作成功时返回受影响的行数
				操作失败时返回false
	*/
	public function dml($sql,$arr){
		$st = $this->p->prepare($sql);
		$st->execute($arr);
		if($this->p->lastInsertId() > 0){
			return $this->p->lastInsertId();
		}else if($st->rowCount() > 0){
			return $st->rowCount();
		}else{
			$err['error'] = 1; 
			$err['code'] = $st->errorCode();
			$err['info'] = $st->errorInfo();
			return $err;
		}
	}

}













